<!DOCTYPE html>
<html lang="en" style="background:#fff;">
	<head>
	<base href="${basePath}">
	<#include "common/top.html"/>
	<link type="text/css" rel="stylesheet" href="${basePath}plugins/zTree/3.5.1/zTreeStyle/zTreeStyle.css"/>
	<script type="text/javascript" src="${basePath}plugins/zTree/3.5.1/jquery.ztree.core.js"></script>
	<script type="text/javascript" src="${basePath}plugins/zTree/3.5.1/jquery.ztree.excheck.js"></script>
    </head>
<body style="background:#fff;">
	<div class="layui-elem-field" style="height: 280px;overflow: auto;">
		<ul id="tree" class="ztree"></ul>
	</div>
	<script type="text/javascript">
		var zTree;
		$(document).ready(function(){
            var zn = '${zTreeNodes}';
            var setting = {
				check: {
					enable: true,
					chkStyle: "checkbox",
					chkboxType: { "Y" : "ps", "N" : "s" }
					/*
					 规则说明
					 Y 属性定义 CheckBox 被勾选后的情况; N 属性定义 CheckBox 取消勾选后的情况; "p" 表示操作会影响父级节点;
					 "s" 表明操作会影响子级节点。
					 */
				},
				data: {
					key: {
						title: "dept"
					},
					simpleData: {
						enable: true,
						idKey: "id",
						pIdKey: "pId",
						rootPId: 0
					}
				},
				callback:{
					onClick: null
				}
			};
			var zNodes = eval(zn);
			zTree = $.fn.zTree.init($("#tree"), setting, zNodes);
		});
		layui.use(['form'], function () {
			var form = layui.form;
			form.render();
		});
		//保存
		 function save(){
			var nodes = zTree.getCheckedNodes();
			var tmpNode;
			var ids = "";
			for(var i=0; i<nodes.length; i++){
				tmpNode = nodes[i];
				if(i!=nodes.length-1){
					ids += tmpNode.id+",";
				}else{
					ids += tmpNode.id;
				}
			}
			var roleId = "${roleId}";
             //更新权限和角色的关系
			var url = "${basePath}system/menu/saveRoleMenus";
			var postData = {"roleId":roleId,"menuIds":ids};
            $.ajax({
				url: url,
                data: postData,
                type: "POST",
			    success: function(data){
					if (data.status == true) {
                        layer.alert(data.msg,function(index){
                            var index1 = parent.layer.getFrameIndex(window.name); //获取窗口索引
                            parent.layer.close(index1);
                            layer.close(index);
                        });
					}else{
						parent.layer.alert(data.msg);
					}
				}
            });

		 }
	
	</script>
</body>
</html>